package com.tgpms.web.earlierStage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.TaskDefinitionTemplate;
import com.tgpms.web.earlierStage.service.EarlierTaskDefinTempService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "EarlierTaskDefinTempController", tags = "前期 任务分解模板相关接口")
@RestController
@RequestMapping(value = "/ev/earlierTaskDefinTemp")
public class EarlierTaskDefinTempController {
	
	@Autowired
	private EarlierTaskDefinTempService earlierTaskDefinTempService;
	@Autowired
	private TokenService tokenService;

	@ApiOperation(value = "新增任务分解模板接口", notes = "新增任务分解模板接口", httpMethod = "POST")
	@PostMapping(value = "/addTaskDefinitionTemplate")
	public Result addTaskDefinitionTemplate(@RequestBody TaskDefinitionTemplate TaskDefinitionTemplate,HttpServletRequest request) {
		Result result = new Result();
		try {
			String insertName = tokenService.getUserName(request);
			TaskDefinitionTemplate.setInsertUser(insertName);
			result = earlierTaskDefinTempService.addTaskDefinTemp(TaskDefinitionTemplate, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "修改任务分解模板接口", notes = "修改任务分解模板接口", httpMethod = "PUT")
	@PutMapping(value = "/updateTaskDefinitionTemplate")
	public Result updateTaskDefinitionTemplate(@RequestBody TaskDefinitionTemplate TaskDefinitionTemplate,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(TaskDefinitionTemplate.getTaskId())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			String insertName = tokenService.getUserName(request);
			TaskDefinitionTemplate.setModifyUser(insertName);
			result = earlierTaskDefinTempService.updateTaskDefinTemp(TaskDefinitionTemplate, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	@ApiOperation(value = "删除任务分解模板接口", notes = "删除任务分解模板接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteTaskDefinitionTemplate")
	public Result deleteTaskDefinitionTemplate(@RequestParam(value = "taskId", required = true)String taskId,
			HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(taskId)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
			return result;
		}
		try {
			result = earlierTaskDefinTempService.deleteTaskDefinTemp(taskId, result);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
	
	@ApiOperation(value = "查询任务分解模板树形接口", notes = "查询任务分解模板树形接口", httpMethod = "POST")
	@PostMapping(value = "/selTaskDefinitionTemplateForTree")
	public Result selTaskDefinitionTemplateForTree(@RequestParam(value = "taskId",required = false) String taskId) {
		Result result = new Result();
		try {
			List<Object> list = earlierTaskDefinTempService.selTaskDefinTemp(taskId);
			result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
	}
	
}
